home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group00b.txt / 000074_icon-group-sender _Wed Oct 11 14:45:48 2000.msg < prev    next >
Internet Message Format  |  2001-01-03  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id OAA08433
  4.     for icon-group-addresses; Wed, 11 Oct 2000 14:45:38 -0700 (MST)
  5. Message-Id: <200010112145.OAA08433@baskerville.CS.Arizona.EDU>
  6. From: Steve Wampler <swampler@noao.edu>
  7. X-Newsgroups: comp.lang.icon
  8. Subject: Re: can icon do this?
  9. Date: Wed, 11 Oct 2000 14:04:26 -0700
  10. X-Trace: noao.edu 971298269 51656 140.252.38.6 (11 Oct 2000 21:04:29 GMT)
  11. X-Complaints-To: abuse@noao.edu
  12. To: Antonella <gea@earthling.net>
  13. X-Accept-Language: en
  14. To: icon-group@optima.CS.Arizona.EDU
  15. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  16. Status: RO
  17. Content-Length: 2569
  18.  
  19. Antonella wrote:
  20. > What code inspects one input text file, checks each line for occurrences
  21. > of a string, and if found replaces that string in an otherwise identical
  22. > output file?
  23.  
  24. There are several parts to this (based on information in a later posting
  25. from Antonella):
  26.  
  27. (a) given s1, s2, and target, produce source so that every occurrence of
  28.     s1 in target has been replaced by s2
  29.  
  30. (b) handle multiple pairs of s1,s2
  31.  
  32. (c) figure out how to specify these multiple pairs on the command line.
  33.  
  34. One solution to (a) is:
  35.  
  36.    procedure mapString(sText, tText, inLine)
  37.        outLine := ""
  38.        inLine ? {
  39.            while outLine ||:= (tab(find(sText)) || tText) do {
  40.                move(*sText)
  41.                }
  42.            outLine ||:= tab(0)
  43.            }
  44.        return outLine
  45.    end
  46.  
  47. (b) is tricky, but one easy to implement approach is to use
  48. a table mapping each s1 into its matching s2.  Assuming
  49. such a table exists, then (a) can be used to help solve (b)
  50. as in:
  51.  
  52.    procedure mapStrings(sMap, inLine)
  53.        outLine := inLine
  54.        every s1 := key(sMap) do {
  55.            outLine := mapString(s1, sMap[s1], outLine)
  56.            }
  57.        return outLine
  58.    end
  59.  
  60. Now the table has to be built.  It's easiest to combine this
  61. with parsing the command-line arguments.  If the commandline
  62. contains arguments of the form s1=s2, then (c) is:
  63.  
  64.     procedure buildMap(args)
  65.        sMap := table()
  66.        every args := !args do {
  67.            s1 := s2 := ""
  68.            arg ? { 
  69.                s1 := tab(upto("=")) | next
  70.                move(1)
  71.                s2 := tab(0)
  72.                }
  73.            if *s1 = 0 then stop("Cannot use null string as a source string")
  74.            sMap[s1] := s2
  75.            }
  76.        return sMap
  77.    end
  78.  
  79. Putting it altogether is a main procedure that reads from
  80. standard input and writes to standard output:
  81.  
  82.    procedure main(args)
  83.        if *args = 0 then stop("Usage: mapFile string1=string2 ...")
  84.  
  85.        sMap := buildMap(args)
  86.        every write(mapStrings(sMap, !&input))
  87.    end
  88.  
  89. If this code is in mapFile.icn and is translated into mapFile, then
  90. an example call would be:
  91.  
  92.    mapFile string=fling arg=barge <mapFile.icn >newFile.icn
  93.  
  94. A potentially more elegant approach (since this one requires multiple
  95. passes over each input lines) is to write a varient of find() that accepts
  96. a set of strings to find and generates the positions of those strings
  97. in increasing order).  Anyone up for it?  Would it be faster, slower,
  98. or about the same as this approach?
  99.  
  100. --
  101. Steve Wampler-  SOLIS Project, National Solar Observatory
  102. swampler@noao.edu
  103.